package com.yilin.tms.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.commons.util.ArrayUtil;
import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.system.system.FunctionApi;
import com.yilin.tms.system.mapper.FunctionApiMapper;
import com.yilin.tms.system.service.IFunctionApiService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class FunctionApiService extends ServiceImpl<FunctionApiMapper, FunctionApi> implements IFunctionApiService {

    @Resource
    FunctionApiMapper functionApiMapper;

    @Override
    public List<FunctionApi> getFunctionApiList(FunctionApi query) {
        if (query.getSort()==null) query.setSort("soft-ASC");
        QueryWrapper<FunctionApi> queryWrapper = QueryUtil.andQueryWrapper(new QueryWrapper<>(),query);
        List<FunctionApi> apiList =  functionApiMapper.selectList(queryWrapper);
        if (ArrayUtil.containsBy(apiList, o -> "0".equals(o.getId()))) return apiList;
        return addParent(apiList);
    }

    @Override
    public List<FunctionApi> getFunctionApiListByCodes(String[] apiCodes) {
        QueryWrapper<FunctionApi> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("soft");
        queryWrapper.in("code", Arrays.asList(apiCodes));
        List<FunctionApi> apiList =  functionApiMapper.selectList(queryWrapper);
        if (ArrayUtil.containsBy(apiList, o -> "0".equals(o.getId()))) return apiList;
        return addParent(apiList);
    }

    // 无论怎么查询 必须返回上级树
    private List<FunctionApi> addParent(List<FunctionApi> apiList) {
        List<FunctionApi> parentList = new ArrayList<>();
        for (FunctionApi api : apiList) excParent(parentList,api);
        return ArrayUtil.mergeList(apiList, parentList, (obj1, obj2) -> obj1.getId().equals(obj2.getId()));
    }

    // 上级树查询
    private List<FunctionApi> excParent(List<FunctionApi> list, FunctionApi api) {
        if (api.getParentId().equals("-1")) return list;
        FunctionApi parent = functionApiMapper.selectById(api.getParentId());
        if (!ArrayUtil.containsBy(list, o -> parent.getId().equals(o.getId()))) list.add(parent);
        return excParent(list,parent);
    }

    @Override
    public void saveFunctionApi(FunctionApi functionApi) {
        if (functionApi.getId()==null){
            functionApi.setCreateTime(new Date());
            functionApiMapper.insert(functionApi);
        }else{
            functionApi.setUpdateTime(new Date());
            functionApiMapper.updateById(functionApi);
        }
        //查询上级结构目录，生成用户标签
        if (functionApi.getApiType()!= FunctionApi.ApiType.catalogue){
            excAttachMaskParent(functionApi);
        }
    }

    /// 自动贴标签
    private boolean excAttachMaskParent(FunctionApi api) {
        if (api.getParentId().equals("-1")) return true;
        FunctionApi parentFunctionApi = functionApiMapper.selectById(api.getParentId());
        if (parentFunctionApi.getTargetUserTypes() == null) parentFunctionApi.setTargetUserTypes("");
        List<String> splitParentTypes = new ArrayList<>();
        /// 刷新上级标签
        List<FunctionApi> childFunctionApiList = getFunctionApiByParentId(parentFunctionApi.getId());
        for (FunctionApi functionApi : childFunctionApiList) {
            if (functionApi.getTargetUserTypes() == null) functionApi.setTargetUserTypes("");
            List<String> userTypes = new ArrayList<>(Arrays.asList(functionApi.getTargetUserTypes().split(",")));
            for (String splitType: userTypes) if (!splitParentTypes.contains(splitType) && StringUtil.isNotEmpty(splitType)) splitParentTypes.add(splitType);
        }
        parentFunctionApi.setTargetUserTypes(String.join(",",splitParentTypes));
        functionApiMapper.updateById(parentFunctionApi);
        return excAttachMaskParent(parentFunctionApi);
    }


    @Override
    public List<FunctionApi> getFunctionApiByParentId(String parentId) {
        QueryWrapper<FunctionApi> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        return functionApiMapper.selectList(queryWrapper);
    }

}
